{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "74b74808-297d-4f15-aceb-533df93acf60",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import os.path\n",
    "import sys\n",
    "sys.path.append(os.path.abspath('..'))\n",
    "from datetime import datetime,timedelta\n",
    "import random\n",
    "import math\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import cnmaps\n",
    "from cnmaps import get_adm_maps, draw_maps\n",
    "from shapely.geometry import Point, Polygon\n",
    "import matplotlib.pyplot as plt\n",
    "import importlib\n",
    "import dateutil.parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "86b1a72e-3a89-4453-b234-0918aed5532d",
   "metadata": {},
   "outputs": [],
   "source": [
    "stations=pd.read_csv('Stations.csv', encoding='utf-8')\n",
    "for c in ['Station_Id_C', 'Province', 'City', 'Cnty', 'Town','Admin_Code_CHN', 'Town_code', 'ProvinceCode', 'CityCode', 'CntyCode',]:\n",
    "    stations[c]=stations[c].apply(lambda x:str(x))\n",
    "stations['City']=stations['City'].apply(lambda x:x if not pd.isna(x) and x!='nan' else np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f8b2c0a5-a2a9-45e6-bdb5-b5caa2a6854e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df=stations.groupby(['ProvinceCode','CityCode'])[['Province', 'City']].agg({\n",
    "    'Province':'first',\n",
    "    'City':'first',\n",
    "}).reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b9053102-2710-49e4-b9b4-4bf32c3a62b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['ProvinceCity']=df.apply(lambda x:x['Province']+x['City'] if not pd.isna(x['City']) and x['City']!='nan' else x['Province'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7f08526d-1f0f-40b6-8522-e56ad6f3f18f",
   "metadata": {},
   "outputs": [],
   "source": [
    "p=df.dropna().reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0b8ef163-5641-42e6-b72c-aeee1fcabdf1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "p=p.to_dict(orient='records')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ba4ca10d-eb48-4a95-a416-b86463defc2f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "dp=dict(zip(map(lambda x:x['ProvinceCity'],p),map(lambda x:x['CityCode'],p)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "337b7c1f-d452-4609-843c-6da6f068061a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "333"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(dp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "311ef9f1-c48a-444a-8f81-b3f198799970",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "citycodes=list(map(lambda x:x['CityCode'],p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "68cebfee-d654-4824-a676-bebe3afb2516",
   "metadata": {},
   "outputs": [],
   "source": [
    "pp=stations[stations['CityCode'].isin(citycodes)&stations['Station_levl']<12].groupby('CityCode')[['Province','City','Station_Id_C']].agg({\n",
    "    'Province':'first',\n",
    "    'City':'first',\n",
    "    'Station_Id_C':'first',\n",
    "}).dropna().reset_index().to_dict(orient='records')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "93d881f8-6dad-4e7d-a187-488e1ed351c8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'河北省石家庄市': '53680',\n",
       " '河北省唐山市': '54429',\n",
       " '河北省秦皇岛市': '54436',\n",
       " '河北省邯郸市': '53773',\n",
       " '河北省邢台市': '53781',\n",
       " '河北省保定市': '53596',\n",
       " '河北省张家口市': '53392',\n",
       " '河北省承德市': '54308',\n",
       " '河北省沧州市': '54610',\n",
       " '河北省廊坊市': '54510',\n",
       " '河北省衡水市': '54606',\n",
       " '山西省太原市': '53669',\n",
       " '山西省大同市': '53486',\n",
       " '山西省阳泉市': '53685',\n",
       " '山西省长治市': '53871',\n",
       " '山西省晋城市': '53970',\n",
       " '山西省朔州市': '53478',\n",
       " '山西省晋中市': '53770',\n",
       " '山西省运城市': '53954',\n",
       " '山西省忻州市': '53564',\n",
       " '山西省临汾市': '53852',\n",
       " '山西省吕梁市': '53659',\n",
       " '内蒙古自治区呼和浩特市': '665254',\n",
       " '内蒙古自治区包头市': '53149',\n",
       " '内蒙古自治区乌海市': '53512',\n",
       " '内蒙古自治区赤峰市': '54024',\n",
       " '内蒙古自治区通辽市': '50924',\n",
       " '内蒙古自治区鄂尔多斯市': '53457',\n",
       " '内蒙古自治区呼伦贝尔市': '50425',\n",
       " '内蒙古自治区巴彦淖尔市': '53231',\n",
       " '内蒙古自治区乌兰察布市': '53362',\n",
       " '内蒙古自治区兴安盟': '50727',\n",
       " '内蒙古自治区锡林郭勒盟': '50913',\n",
       " '内蒙古自治区阿拉善盟': '52267',\n",
       " '辽宁省沈阳市': '54244',\n",
       " '辽宁省大连市': '54563',\n",
       " '辽宁省鞍山市': '54336',\n",
       " '辽宁省抚顺市': '54259',\n",
       " '辽宁省本溪市': '54346',\n",
       " '辽宁省丹东市': '54493',\n",
       " '辽宁省锦州市': '54330',\n",
       " '辽宁省营口市': '54471',\n",
       " '辽宁省阜新市': '54236',\n",
       " '辽宁省辽阳市': '54345',\n",
       " '辽宁省盘锦市': '54470',\n",
       " '辽宁省铁岭市': '54243',\n",
       " '辽宁省朝阳市': '54323',\n",
       " '辽宁省葫芦岛市': '54452',\n",
       " '吉林省长春市': '54064',\n",
       " '吉林省吉林市': '54076',\n",
       " '吉林省四平市': '54142',\n",
       " '吉林省辽源市': '54260',\n",
       " '吉林省通化市': '54266',\n",
       " '吉林省白山市': '54276',\n",
       " '吉林省松原市': '50946',\n",
       " '吉林省白城市': '50936',\n",
       " '吉林省延边朝鲜族自治州': '54186',\n",
       " '黑龙江省哈尔滨市': '50867',\n",
       " '黑龙江省齐齐哈尔市': '670067',\n",
       " '黑龙江省鸡西市': '50978',\n",
       " '黑龙江省鹤岗市': '50775',\n",
       " '黑龙江省双鸭山市': '50880',\n",
       " '黑龙江省大庆市': '50749',\n",
       " '黑龙江省伊春市': '50673',\n",
       " '黑龙江省佳木斯市': '50778',\n",
       " '黑龙江省七台河市': '50971',\n",
       " '黑龙江省牡丹江市': '50979',\n",
       " '黑龙江省黑河市': '50468',\n",
       " '黑龙江省绥化市': '50756',\n",
       " '黑龙江省大兴安岭地区': '50136',\n",
       " '江苏省南京市': '58235',\n",
       " '江苏省无锡市': '58346',\n",
       " '江苏省徐州市': '58012',\n",
       " '江苏省常州市': '58342',\n",
       " '江苏省苏州市': '58349',\n",
       " '江苏省南通市': '58254',\n",
       " '江苏省连云港市': '58036',\n",
       " '江苏省淮安市': '58138',\n",
       " '江苏省盐城市': '58045',\n",
       " '江苏省扬州市': '58148',\n",
       " '江苏省镇江市': '58247',\n",
       " '江苏省泰州市': '58243',\n",
       " '江苏省宿迁市': '58038',\n",
       " '浙江省杭州市': '58444',\n",
       " '浙江省宁波市': '58467',\n",
       " '浙江省温州市': '58656',\n",
       " '浙江省嘉兴市': '58451',\n",
       " '浙江省湖州市': '58443',\n",
       " '浙江省绍兴市': '58453',\n",
       " '浙江省金华市': '58546',\n",
       " '浙江省衢州市': '58537',\n",
       " '浙江省舟山市': '58472',\n",
       " '浙江省台州市': '58559',\n",
       " '浙江省丽水市': '58644',\n",
       " '安徽省合肥市': '58220',\n",
       " '安徽省芜湖市': '58329',\n",
       " '安徽省蚌埠市': '58127',\n",
       " '安徽省淮南市': '58212',\n",
       " '安徽省马鞍山市': '58330',\n",
       " '安徽省淮北市': '58113',\n",
       " '安徽省铜陵市': '58420',\n",
       " '安徽省安庆市': '58317',\n",
       " '安徽省黄山市': '58426',\n",
       " '安徽省滁州市': '58222',\n",
       " '安徽省阜阳市': '58107',\n",
       " '安徽省宿州市': '58015',\n",
       " '安徽省六安市': '58214',\n",
       " '安徽省亳州市': '58102',\n",
       " '安徽省池州市': '58419',\n",
       " '安徽省宣城市': '58432',\n",
       " '福建省福州市': '58839',\n",
       " '福建省厦门市': '59130',\n",
       " '福建省莆田市': '58936',\n",
       " '福建省三明市': '58818',\n",
       " '福建省泉州市': '58929',\n",
       " '福建省漳州市': '58928',\n",
       " '福建省南平市': '58724',\n",
       " '福建省龙岩市': '58911',\n",
       " '福建省宁德市': '58744',\n",
       " '江西省南昌市': '58602',\n",
       " '江西省景德镇市': '58524',\n",
       " '江西省萍乡市': '57783',\n",
       " '江西省九江市': '57598',\n",
       " '江西省新余市': '57792',\n",
       " '江西省鹰潭市': '58616',\n",
       " '江西省赣州市': '57990',\n",
       " '江西省吉安市': '57798',\n",
       " '江西省宜春市': '57694',\n",
       " '江西省抚州市': '58618',\n",
       " '江西省上饶市': '58519',\n",
       " '山东省济南市': '54724',\n",
       " '山东省青岛市': '54842',\n",
       " '山东省淄博市': '54729',\n",
       " '山东省枣庄市': '54927',\n",
       " '山东省东营市': '54731',\n",
       " '山东省烟台市': '54749',\n",
       " '山东省潍坊市': '54831',\n",
       " '山东省济宁市': '54907',\n",
       " '山东省泰安市': '54819',\n",
       " '山东省威海市': '54774',\n",
       " '山东省日照市': '54936',\n",
       " '山东省临沂市': '54923',\n",
       " '山东省德州市': '54709',\n",
       " '山东省聊城市': '54802',\n",
       " '山东省滨州市': '54722',\n",
       " '山东省菏泽市': '54904',\n",
       " '河南省郑州市': '57080',\n",
       " '河南省开封市': '57091',\n",
       " '河南省洛阳市': '57065',\n",
       " '河南省平顶山市': '57075',\n",
       " '河南省安阳市': '53889',\n",
       " '河南省鹤壁市': '53974',\n",
       " '河南省新乡市': '53983',\n",
       " '河南省焦作市': '53972',\n",
       " '河南省濮阳市': '54817',\n",
       " '河南省许昌市': '57087',\n",
       " '河南省漯河市': '57183',\n",
       " '河南省三门峡市': '57051',\n",
       " '河南省南阳市': '57156',\n",
       " '河南省商丘市': '58001',\n",
       " '河南省信阳市': '57296',\n",
       " '河南省周口市': '57098',\n",
       " '河南省驻马店市': '57188',\n",
       " '湖北省武汉市': '57489',\n",
       " '湖北省黄石市': '57499',\n",
       " '湖北省十堰市': '57249',\n",
       " '湖北省宜昌市': '57358',\n",
       " '湖北省襄阳市': '57265',\n",
       " '湖北省鄂州市': '57496',\n",
       " '湖北省荆门市': '57377',\n",
       " '湖北省孝感市': '57388',\n",
       " '湖北省荆州市': '57469',\n",
       " '湖北省黄冈市': '57398',\n",
       " '湖北省咸宁市': '57582',\n",
       " '湖北省随州市': '57381',\n",
       " '湖北省恩施土家族苗族自治州': '57355',\n",
       " '湖南省长沙市': '57678',\n",
       " '湖南省株洲市': '57779',\n",
       " '湖南省湘潭市': '57771',\n",
       " '湖南省衡阳市': '57776',\n",
       " '湖南省邵阳市': '57758',\n",
       " '湖南省岳阳市': '57575',\n",
       " '湖南省常德市': '57562',\n",
       " '湖南省张家界市': '57554',\n",
       " '湖南省益阳市': '57574',\n",
       " '湖南省郴州市': '57881',\n",
       " '湖南省永州市': '57865',\n",
       " '湖南省怀化市': '57655',\n",
       " '湖南省娄底市': '57760',\n",
       " '湖南省湘西土家族苗族自治州': '57544',\n",
       " '广东省广州市': '59284',\n",
       " '广东省韶关市': '57988',\n",
       " '广东省深圳市': '59493',\n",
       " '广东省珠海市': '59487',\n",
       " '广东省汕头市': '59316',\n",
       " '广东省佛山市': '59279',\n",
       " '广东省江门市': '59473',\n",
       " '广东省湛江市': '59650',\n",
       " '广东省茂名市': '59456',\n",
       " '广东省肇庆市': '59264',\n",
       " '广东省惠州市': '59290',\n",
       " '广东省梅州市': '59106',\n",
       " '广东省汕尾市': '59500',\n",
       " '广东省河源市': '59096',\n",
       " '广东省阳江市': '59469',\n",
       " '广东省清远市': '59071',\n",
       " '广东省东莞市': '59289',\n",
       " '广东省中山市': '59485',\n",
       " '广东省潮州市': '59312',\n",
       " '广东省揭阳市': '59306',\n",
       " '广东省云浮市': '59268',\n",
       " '广西壮族自治区南宁市': '59229',\n",
       " '广西壮族自治区柳州市': '57941',\n",
       " '广西壮族自治区桂林市': '57859',\n",
       " '广西壮族自治区梧州市': '59058',\n",
       " '广西壮族自治区北海市': '59640',\n",
       " '广西壮族自治区防城港市': '59429',\n",
       " '广西壮族自治区钦州市': '59446',\n",
       " '广西壮族自治区贵港市': '59249',\n",
       " '广西壮族自治区玉林市': '59449',\n",
       " '广西壮族自治区百色市': '59001',\n",
       " '广西壮族自治区贺州市': '59059',\n",
       " '广西壮族自治区河池市': '57927',\n",
       " '广西壮族自治区来宾市': '59038',\n",
       " '广西壮族自治区崇左市': '59227',\n",
       " '海南省海口市': '59758',\n",
       " '海南省三亚市': '59948',\n",
       " '海南省三沙市': '771901',\n",
       " '海南省儋州市': '59845',\n",
       " '四川省成都市': '56181',\n",
       " '四川省自贡市': '56394',\n",
       " '四川省攀枝花市': '56665',\n",
       " '四川省泸州市': '57508',\n",
       " '四川省德阳市': '56186',\n",
       " '四川省绵阳市': '56190',\n",
       " '四川省广元市': '57204',\n",
       " '四川省遂宁市': '57401',\n",
       " '四川省内江市': '56393',\n",
       " '四川省乐山市': '56382',\n",
       " '四川省南充市': '57306',\n",
       " '四川省眉山市': '56289',\n",
       " '四川省宜宾市': '56491',\n",
       " '四川省广安市': '57414',\n",
       " '四川省达州市': '57237',\n",
       " '四川省雅安市': '56273',\n",
       " '四川省巴中市': '57216',\n",
       " '四川省资阳市': '56298',\n",
       " '四川省阿坝藏族羌族自治州': '56079',\n",
       " '四川省甘孜藏族自治州': '56038',\n",
       " '四川省凉山彝族自治州': '56459',\n",
       " '贵州省贵阳市': '57718',\n",
       " '贵州省六盘水市': '56693',\n",
       " '贵州省遵义市': '57606',\n",
       " '贵州省安顺市': '57806',\n",
       " '贵州省毕节市': '56598',\n",
       " '贵州省铜仁市': '57636',\n",
       " '贵州省黔西南布依族苗族自治州': '786329',\n",
       " '贵州省黔东南苗族侗族自治州': '57735',\n",
       " '贵州省黔南布依族苗族自治州': '57728',\n",
       " '云南省昆明市': '56688',\n",
       " '云南省曲靖市': '786068',\n",
       " '云南省玉溪市': '56869',\n",
       " '云南省保山市': '56739',\n",
       " '云南省昭通市': '835428',\n",
       " '云南省丽江市': '56567',\n",
       " '云南省普洱市': '56856',\n",
       " '云南省临沧市': '56839',\n",
       " '云南省楚雄彝族自治州': '56669',\n",
       " '云南省红河哈尼族彝族自治州': '56885',\n",
       " '云南省文山壮族苗族自治州': '56889',\n",
       " '云南省西双版纳傣族自治州': '56958',\n",
       " '云南省大理白族自治州': '56646',\n",
       " '云南省德宏傣族景颇族自治州': '56835',\n",
       " '云南省怒江傈僳族自治州': '56533',\n",
       " '云南省迪庆藏族自治州': '56444',\n",
       " '西藏自治区拉萨市': '55493',\n",
       " '西藏自治区日喀则市': '55542',\n",
       " '西藏自治区昌都市': '56116',\n",
       " '西藏自治区林芝市': '56227',\n",
       " '西藏自治区山南市': '55589',\n",
       " '西藏自治区那曲市': '55178',\n",
       " '西藏自治区阿里地区': '55125',\n",
       " '陕西省西安市': '57032',\n",
       " '陕西省铜川市': '53945',\n",
       " '陕西省宝鸡市': '57003',\n",
       " '陕西省咸阳市': '53929',\n",
       " '陕西省渭南市': '53941',\n",
       " '陕西省延安市': '53738',\n",
       " '陕西省汉中市': '57106',\n",
       " '陕西省榆林市': '53567',\n",
       " '陕西省安康市': '57137',\n",
       " '陕西省商洛市': '57057',\n",
       " '甘肃省兰州市': '52884',\n",
       " '甘肃省嘉峪关市': '871601',\n",
       " '甘肃省金昌市': '52674',\n",
       " '甘肃省白银市': '52797',\n",
       " '甘肃省天水市': '57001',\n",
       " '甘肃省武威市': '52679',\n",
       " '甘肃省张掖市': '675004',\n",
       " '甘肃省平凉市': '53906',\n",
       " '甘肃省酒泉市': '52418',\n",
       " '甘肃省庆阳市': '53821',\n",
       " '甘肃省定西市': '52986',\n",
       " '甘肃省陇南市': '56095',\n",
       " '甘肃省临夏回族自治州': '52980',\n",
       " '甘肃省甘南藏族自治州': '52978',\n",
       " '青海省西宁市': '52855',\n",
       " '青海省海东市': '52863',\n",
       " '青海省海北藏族自治州': '52657',\n",
       " '青海省黄南藏族自治州': '52963',\n",
       " '青海省海南藏族自治州': '52856',\n",
       " '青海省果洛藏族自治州': '56033',\n",
       " '青海省玉树藏族自治州': '56016',\n",
       " '青海省海西蒙古族藏族自治州': '51886',\n",
       " '宁夏回族自治区银川市': '53610',\n",
       " '宁夏回族自治区石嘴山市': '53518',\n",
       " '宁夏回族自治区吴忠市': '53612',\n",
       " '宁夏回族自治区固原市': '53817',\n",
       " '宁夏回族自治区中卫市': '53704',\n",
       " '新疆维吾尔自治区乌鲁木齐市': '51369',\n",
       " '新疆维吾尔自治区克拉玛依市': '51243',\n",
       " '新疆维吾尔自治区吐鲁番市': '51571',\n",
       " '新疆维吾尔自治区哈密市': '52101',\n",
       " '新疆维吾尔自治区昌吉回族自治州': '51353',\n",
       " '新疆维吾尔自治区博尔塔拉蒙古自治州': '51232',\n",
       " '新疆维吾尔自治区巴音郭楞蒙古自治州': '51467',\n",
       " '新疆维吾尔自治区阿克苏地区': '51627',\n",
       " '新疆维吾尔自治区克孜勒苏柯尔克孜自治州': '51704',\n",
       " '新疆维吾尔自治区喀什地区': '51707',\n",
       " '新疆维吾尔自治区和田地区': '51818',\n",
       " '新疆维吾尔自治区伊犁哈萨克自治州': '51328',\n",
       " '新疆维吾尔自治区塔城地区': '51133',\n",
       " '新疆维吾尔自治区阿勒泰地区': '51053'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(zip(map(lambda x:x['Province']+x['City'],p),map(lambda x:x['Station_Id_C'],pp)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a57e098-c300-4485-b6f2-864cab2955dc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "dpp=dict(zip(map(lambda x:x['CityCode'],p),map(lambda x:x['Station_Id_C'],pp)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fd94b59-a84a-4ed2-939e-3d8165a3118c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "stations[stations['CityCode'].isin(citycodes)&stations['Station_levl']<12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a286f12-c5e6-4aee-a5df-0b023b1a0239",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1e9c30a-2447-4538-829a-85ed262de0b7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f21c0c42-ac79-4960-b1b9-733aa839b45c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d69461b-b0db-4f53-bc94-22182cf0c264",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0abf87cd-d5dd-4e38-acf3-32d56d7cea2e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:webdev]",
   "language": "python",
   "name": "conda-env-webdev-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
